//
//  WrongQuestionDetailController.m
//  Learn
//
//  Created by 迟建国 on 2024/4/4.
//  Copyright © 2024 迟建国. All rights reserved.
//

#import "WrongQuestionDetailController.h"
#import "WrongQuestionItemTableViewCell.h"
#import "QuestionChooseView.h"
#import "AlertView.h"
#import "WrongQuestionSubmitController.h"

@interface WrongQuestionDetailController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation WrongQuestionDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"sdsdsdsdds";
    self.view.backgroundColor = rgba(245, 245, 245, 1);
    [self setUI];
    [self setNavUI];
    // Do any additional setup after loading the view.
}

- (void)setUI {
    
    self.tableView=[[UITableView alloc]initWithFrame:CGRectMake(0,NavigationBarHeight, SCREEN_W, SCREEN_H - NavigationBarHeight - SafeAreaBottomHeight - 126) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    if (@available(iOS 13.0, *)) {
        [self.tableView setAutomaticallyAdjustsScrollIndicatorInsets:NO];
    } else {
        // Fallback on earlier versions
    }
    if (@available(iOS 15.0, *)) {
        self.tableView.sectionHeaderTopPadding = 0;
    } else {
        // Fallback on earlier versions
    }
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    self.tableView.backgroundColor=[UIColor clearColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
        self.tableView.estimatedRowHeight = 180;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.showsHorizontalScrollIndicator = NO;
    [self.tableView registerNib:[UINib nibWithNibName:@"WrongQuestionItemTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"WrongQuestionItemTableViewCell"];

    UIButton *listenBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [listenBtn setImage:[UIImage imageNamed:@"icon_play"] forState:UIControlStateNormal];
    [listenBtn bk_whenTapped:^{
        [AlertView alertWith:self title:@"" content:@"本次学习尚未结束，\n是否确定退出？" cancel:@"取消" sure:@"确定并保存" Complection:^{
            WrongQuestionSubmitController *vc = [[WrongQuestionSubmitController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
            
        }];
    }];
    [self.view addSubview:listenBtn];
    [listenBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.tableView.mas_bottom).offset(16);
        make.centerX.equalTo(self.tableView.mas_centerX);
        make.width.height.offset(52);
    }];
    
    UIButton *nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [nextBtn setTitle:@"下一步" forState:UIControlStateNormal];
    [nextBtn setTitleColor:rgba(12, 183, 108, 1) forState:UIControlStateNormal];
    nextBtn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:16];
    [nextBtn bk_whenTapped:^{
        [QuestionChooseView chooseQuestionWithVc:self Complection:^(NSString * _Nonnull str) {
            
        }];
    }];
    [self.view addSubview:nextBtn];
    [nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view.mas_right).offset(-56);
        make.centerY.equalTo(listenBtn.mas_centerY);
        make.height.offset(44);
    }];
    
    UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_H - SafeAreaBottomHeight - 28, SCREEN_W, 28)];
    bottomView.backgroundColor = rgba(12, 183, 108, 0.1);
    [self.view addSubview:bottomView];
    
    UILabel *timeL = [[UILabel alloc] init];
    timeL.text = @"准备时间 剩余22s";
    timeL.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:12];
    timeL.textColor = rgba(51, 51, 51, 1);
    [bottomView addSubview:timeL];
    [timeL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(bottomView.mas_centerX);
        make.centerY.equalTo(bottomView.mas_centerY);
    }];
}
- (void)setNavUI {
    UIView *nav = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, NavigationBarHeight)];
    nav.backgroundColor = rgba(245, 245, 245, 1);
    [self.view addSubview:nav];
//    self.nav = nav;
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn setImage:[UIImage imageNamed:@"icon_back"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [nav addSubview:backBtn];
    [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(nav.mas_left).offset(5);
        make.bottom.equalTo(nav.mas_bottom);
        make.width.offset(50);
        make.height.offset(44);
    }];
    
    UILabel *titleL = [[UILabel alloc] init];
    titleL.text = @"self.model.resourceName";
    titleL.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:16];
    titleL.textColor = rgb(51, 51, 51);
    titleL.textAlignment = NSTextAlignmentCenter;
    [nav addSubview:titleL];
    [titleL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(nav.mas_bottom);
        make.centerX.equalTo(nav.mas_centerX);
        make.height.offset(44);
    }];
    
    UIButton *saveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [saveBtn setImage:[UIImage imageNamed:@"icon_save"] forState:UIControlStateNormal];
    [saveBtn addTarget:self action:@selector(save) forControlEvents:UIControlEventTouchUpInside];
    [nav addSubview:saveBtn];
    [saveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view.mas_right).offset(-10);
        make.bottom.equalTo(nav.mas_bottom);
        make.height.offset(44);
    }];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    WrongQuestionItemTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WrongQuestionItemTableViewCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"WrongQuestionItemTableViewCell" owner:self options:nil] lastObject];
    }
    return cell;
}

- (void)save {
//    SubmitListenTestViewController *vc = [[SubmitListenTestViewController alloc] init];
//    [self.navigationController pushViewController:vc animated:YES];
}
- (void)back {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

@end
